﻿using FBroSharp.Callback;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace BaseTest
{
    //清理缓存回调
    public class ClearCacheCallback : IFBroSharpClearCacheCallback
    {
        public int flag = 0;

        void IFBroSharpClearCacheCallback.DoFinish(bool success)
        {
            Console.WriteLine("清理缓存：" + flag.ToString() + " " + success);
        }
    }


    //下载图片回调
    public class FBroSharpDownloadImageCallback : IFBroSharpDownloadImageCallback
    {

        void IFBroSharpDownloadImageCallback.OnDownloadImageFinished(string image_url, int http_status_code, IFBroSharpImage image)
        {
            int pixel_width = 0;
            int pixel_height = 0;
            IFBroSharpBinaryValue data = image.GetAsPNG(0, true, ref pixel_width, ref pixel_height);

            //取出数据大小
            int size = data.GetSize();

            var bytes = data.GetByte();

            /* 利用原始指针的方式取数据
            //分配存放数据的指针，这里用完了要释放
            IntPtr intPtr = Marshal.AllocHGlobal(size);

            List<byte> tb = new List<byte>();

            //获取图片数据
            data.GetData(intPtr, (uint)size, 0);

            //创建byte数据保存区域
            byte[] bytes = new byte[size];

            //将图片数据复制到bytes中
            Marshal.Copy(intPtr, bytes, 0, size);

            //清理释放intPtr数据
            Marshal.FreeHGlobal(intPtr);

            ////创建图片文件
            //FileStream file = new FileStream("1.png", FileMode.Create, FileAccess.Write);

            ////向文件写入图片数据
            //file.Write(bytes, 0, size);

            ////关闭文件
            //file.Close();
            */

            //将bytes保存到流中
            MemoryStream ms = new MemoryStream(bytes)
            {
                Position = 0
            };
            //将数据转换成Image数据
            Image img = Image.FromStream(ms);

            //创建图片窗口
            var image_form = new ImageForm
            {

                //将图片数据保存到窗口变量中
                image_data_ = img
            };

            //显示窗口
            image_form.Show();

            Console.WriteLine("尺寸：" + size + " 宽度：" + pixel_width + " 高度：" + pixel_height);

        }
    }

    //打开对话框回调
    public class FBroSharpFileDialogCallback : IFBroSharpFileDialogCallback
    {
        void IFBroSharpFileDialogCallback.OnFileDialogDismissed(string file_paths)
        {
            Console.WriteLine(file_paths);
        }
    }


    public class FBroSharpPdfPrintCallback : IFBroSharpPdfPrintCallback
    {
        void IFBroSharpPdfPrintCallback.OnPdfPrintFinished(string path, bool ok)
        {
            Console.WriteLine(path + " 结果:" + ok);
        }
    }


    public class FBroSharpStringVisitor : IFBroSharpStringVisitor
    {
        public string type;
        void IFBroSharpStringVisitor.Visit(string data)
        {
            Console.WriteLine(type + ":" + data);
        }
    }

    public class FBroSharpJsCallback : IFBroSharpJsCallback
    {

        void IFBroSharpJsCallback.Callback(FBroSharpJSRequestType type, IFBroSharpValue data, string error)
        {
            switch (type)
            {
                case FBroSharpJSRequestType.VALUE_ISVALID:
                    //无效
                    Console.WriteLine(error);
                    break;
                case FBroSharpJSRequestType.VALUE_NULL:
                    //空值
                    Console.WriteLine("无值");
                    break;
                case FBroSharpJSRequestType.VALUE_INT:
                    //整数值
                    Console.WriteLine(data.GetInt());
                    break;
                case FBroSharpJSRequestType.VALUE_STRING:
                    //文本值
                    Console.WriteLine(data.GetString());
                    break;
                case FBroSharpJSRequestType.VALUE_BOOL:
                    //逻辑值
                    Console.WriteLine(data.GetBool());
                    break;
                case FBroSharpJSRequestType.VALUE_DOUBLE:
                    Console.WriteLine(data.GetDouble());
                    //双精度小数值
                    break;
                case FBroSharpJSRequestType.VALUE_DATA_STRING:
                    Console.WriteLine(data.GetString());
                    //时间文本值
                    break;
                case FBroSharpJSRequestType.VALUE_ARRAY:
                    Console.WriteLine(data.GetString());
                    //数组值
                    break;
                case FBroSharpJSRequestType.VALUE_ARRAY_BUFFER:
                    Console.WriteLine(data.GetString());
                    //数组缓存值
                    break;
                case FBroSharpJSRequestType.VALUE_FUNCTION:
                    Console.WriteLine(data.GetString());
                    //方法值
                    break;
                case FBroSharpJSRequestType.VALUE_OBJECT:
                    //类值
                    Console.WriteLine(data.GetString());
                    break;
            }
        }
    }


    class FBroSharpCookieVisitor : IFBroSharpCookieVisitor
    {
        bool IFBroSharpCookieVisitor.Visit(FBroSharpCookieData cookie, int count, int total, ref bool deleteCookie)
        {

            Console.WriteLine("name:" + cookie.name + " value:" + cookie.value + " domain:" + cookie.domain
                + " expires_time:" + cookie.expires_time + " last_access_time:" + cookie.last_access_time
                + " 当前:" + count + " 合计:" + total);

            //不删除当前cookie
            deleteCookie = false;

            //返回真继续，返回假结束不在获取
            return true;
        }
    };

    //服务器回调
    class FBroSharpServerHandle : IFBroSharpServerHandle
    {
        const string format = "方法:{0} 地址:{1}";
        public void OnClientConnected(IFBroSharpServer server, int connection_id)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnClientDisconnected(IFBroSharpServer server, int connection_id)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnHttpRequest(IFBroSharpServer server, int connection_id, string client_address, IFBroSharpRequest request)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnServerCreated(IFBroSharpServer server)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnServerDestroyed(IFBroSharpServer server)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnWebSocketConnected(IFBroSharpServer server, int connection_id)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }

        public void OnWebSocketMessage(IFBroSharpServer server, int connection_id, byte[] data)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
            server.SendWebSocketMessage(connection_id, data);
        }

        public void OnWebSocketRequest(IFBroSharpServer server, int connection_id, string client_address, IFBroSharpRequest request, IFBroSharpCallback callback)
        {
            Console.WriteLine(String.Format(format, MethodBase.GetCurrentMethod().Name, server.GetAddress()));
        }
    }


    public class FBroSharpJsCallbackSyn : IFBroSharpJsCallback
    {
        //同步辅助类
        public FBroSharpSynHelp help;
        public FBroSharpJsCallbackSyn()
        {
            //初始化help类
            help = new FBroSharpSynHelp();
        }

        void IFBroSharpJsCallback.Callback(FBroSharpJSRequestType type, IFBroSharpValue data, string error)
        {

            //将返回的数据保存在help中
            if (help != null && help.IsValid)
            {
                switch (type)
                {
                    case FBroSharpJSRequestType.VALUE_ISVALID:
                        //无效
                        help.SetStringData(error);
                        break;
                    case FBroSharpJSRequestType.VALUE_NULL:
                        //空值
                        help.SetStringData("无值");
                        break;
                    case FBroSharpJSRequestType.VALUE_INT:
                        //整数值
                        help.SetIntData(data.GetInt());
                        break;
                    case FBroSharpJSRequestType.VALUE_STRING:
                        //文本值
                        help.SetStringData(data.GetString());
                        break;
                    case FBroSharpJSRequestType.VALUE_BOOL:
                        //逻辑值
                        help.SetBoolData(data.GetBool());
                        break;
                    case FBroSharpJSRequestType.VALUE_DOUBLE:
                        help.SetDoubleData(data.GetDouble());
                        //双精度小数值
                        break;
                    case FBroSharpJSRequestType.VALUE_DATA_STRING:
                        help.SetStringData(data.GetString());
                        //时间文本值
                        break;
                    case FBroSharpJSRequestType.VALUE_ARRAY:
                        help.SetStringData(data.GetString());
                        //数组值
                        break;
                    case FBroSharpJSRequestType.VALUE_ARRAY_BUFFER:
                        help.SetStringData(data.GetString());
                        //数组缓存值
                        break;
                    case FBroSharpJSRequestType.VALUE_FUNCTION:
                        help.SetStringData(data.GetString());
                        //方法值
                        break;
                    case FBroSharpJSRequestType.VALUE_OBJECT:
                        //类值
                        help.SetStringData(data.GetString());
                        break;
                }
                //停止等待

                help.StopEvent();
            }
        }
    }

}
